create trigger tr_TrangThaiTaiSan on TaiSan
for insert,update
as begin
	declare @id int,@ThoiGianBatDauSuDung datetime,@ThoiGianKhauHao int,@namMoi datetime,@ngayHeThong datetime
	set @id=(select id from inserted)
	set @ThoiGianBatDauSuDung=(select NgayTinhKhauHao from inserted where id=@id)
	set @ThoiGianKhauHao=(select ThoiGianKhauHao from inserted where id=@id)
	set @namMoi=(select DATEADD(yyyy,@ThoiGianKhauHao,@ThoiGianBatDauSuDung) from inserted where Id=@id)
	set @ngayHeThong=(select GETDATE() as ngayHeThong)
	if(select DATEDIFF(DD,@namMoi,@ngayHeThong))>0
	begin
		update TaiSan set TrangThai=0 where ID=@id
	end
	else
	begin
		update TaiSan set TrangThai=1 where ID=@id
	end
end